import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, Router } from '@angular/router';

import { AuthService } from '../auth/auth.service';

@Injectable({
    providedIn: 'root'
})
export class AclGuard implements CanActivate {
    constructor(private router: Router, private authService: AuthService) { }

    // 拿到需要跳转到登录才能访问的路由
    canActivate(next: ActivatedRouteSnapshot) {
        let permissAccess = false;
        const roles = next.data.roles;
        // 页面不需要权限限制或用户角色信息符合权限条件就允许显示页面
        if (roles == null || this.authService.isAuthorized(roles)) {
            permissAccess = true;
        }
        if (!permissAccess) {
            this.router.navigate(['/login']);
        }
        return permissAccess;
    }
}
